package org.gocom.components.conframe.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import java.text.SimpleDateFormat;

import commonj.sdo.DataObject;
import commonj.sdo.Type;
import commonj.sdo.Property;

import com.eos.foundation.data.DataObjectUtil;
import com.eos.system.annotation.Bizlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
/**
 * JSON数据格式转换
 * @author Even
 *
 */
@Bizlet("JSON 工具类")
public class JSONUtil {
	
	/**
	 * 将Collection数据转换为JSON字符串
	 * @param data Collection 集合
	 * @param page 分页数据
	 * @return
	 */
	public static String toJSONString(Collection data,DataObject page){
		Map<String,Object> dataMap = new HashMap<String,Object>();
		if(page!=null){
			dataMap.put("total", page.getInt("count"));
		}
		if(data!=null){
			dataMap.put("rows", data);
		}
		return JSON.toJSONString(dataMap);
	}
	
	/**
	 * 将Object数据转换为JSON字符串
	 * @param data Object数组
	 * @param page 分页数据
	 * @return
	 */
	public static String toJSONString(Object[] data,DataObject page){
		Map<String,Object> dataMap = new HashMap<String,Object>();
		if(page!=null){
			dataMap.put("total", page.getInt("count"));
		}
		if(data!=null){
			dataMap.put("rows", data);
		}
		return JSON.toJSONString(dataMap);
	}
	
	/**
	 * 将DataObject数据转换为JSON字符串
	 * @param data DataObject数组
	 * @param page 分页数据
	 * @return
	 */
	public static String toJSONString(DataObject[] data,DataObject page){
		Map<String,Object> dataMap = new HashMap<String,Object>();
		if(page!=null){
			dataMap.put("total", page.getInt("count"));
		}
		if(data!=null){
			dataMap.put("rows", toList(data));
		}
		return JSON.toJSONString(dataMap);
	}
	
	/**
	 * 将DataObject数据转换为JSON字符串
	 * @param data DataObject
	 * @return
	 */
	public static String toJSONString(DataObject data){
		return JSON.toJSONString(toMap(data));
	}
	
	/**
	 * 将DataObject数组转换为List<Map>
	 * @param data DataObject数组
	 * @return
	 */
	public static List<Map> toList(DataObject[] data){
		List<Map> list = new ArrayList<Map>();
		for(int i=0;i<data.length;i++){
			list.add(toMap(data[i]));
		}
		return list;
	}
	/**
	 * @author guanwancheng
	 * 将DataObject数组转换为List<Map>
	 * @param data DataObject数组
	 * @param dataName DataObject的参数数组 例如{emp,empcode,......}
	 * @return
	 */
	@Bizlet("将DataObject数组转换为List<Map>")
	public static List<String[]> DataObjectToList(DataObject[] data,String[] dataName) throws Exception{
		List<String[]> list = new ArrayList<String[]>();
		DataObject[] data1;
		Object emp;
		for(int i=0;i<data.length;i++){
			String str[] = new String[dataName.length];
			for(int j=0;j<dataName.length;j++){
				str[j]=data[i].getString(dataName[j]);
			}
				list.add(str);
			//list.add(toMap(data[i]));
		}
		return list;
	}
	
	
	/**
	 * @author guanwancheng
	 * 将array转换为List<Map>
	 * @param data DataObject数组
	 * @param 
	 * @return
	 */
	@Bizlet("将array转换为List<Map>")
	public static List<String[]> arrayToList(List[] data) throws Exception{
		List<String[]> list = new ArrayList<String[]>();
		for(int i=0;i<data.length;i++){
			String str[] = new String[data[i].size()];
			for(int j=0;j<=data[i].size()-1;j++){
				System.out.println(data[i].size());
				str[j]=(String) data[i].get(j);
			}
			list.add(str);
		}
		return list;
	}
	
	
	
	/**
	 * 将DataObject数据转换为Map
	 * @param obj DataObject数据
	 * @return
	 */
	public static Map toMap(DataObject obj){
		Map<String,Object> map = new HashMap<String,Object>();
		Type type = obj.getType();
		List properties = type.getProperties();
		for(int j=0;j<properties.size();j++){
			if(obj.isSet(j)){
				Property p = (Property)properties.get(j);
				Object v = obj.get(j);
				String pn = p.getName();
				if(p.getType().getInstanceClass()!=null){
					if(p.getType().getInstanceClass().equals(java.util.Date.class)){
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
						map.put(pn, v==null?"":sdf.format(v));
					}else
						map.put(pn, v);
				}else{
					map.put(pn, toMap((DataObject)v));
				}
			}
		}
		return map;
	}
	
	/**
	 * 将JSONArray转换为DataObject数组
	 * @param array JSONArray
	 * @param entity 数据实体全名
	 * @return
	 */
	public static DataObject[] parseJSONArray2DataObjectArray(JSONArray array,String entity){
		if(array==null||array.size()==0)
			return null;
		DataObject[] entities = new DataObject[array.size()];
		for(int i=0;i<array.size();i++){
			DataObject data = DataObjectUtil.createDataObject(entity);
			JSONObject json = array.getJSONObject(i);
			Iterator<String> it = json.keySet().iterator();
			while(it.hasNext()){
				String key = it.next();
				
				if(json.get(key)!=null&&json.get(key) instanceof JSONObject){
					JSONObject obj = json.getJSONObject(key);
					if(obj.isEmpty())
						continue;
					Property p = data.getProperty(key);
					String en = data.getType().getURI()+"."+p.getType().getName();
					DataObject sub = DataObjectUtil.createDataObject(en);
					if(sub==null)
						data.set(key, parseJSON2Map(json.getJSONObject(key)));
					else{
						data.set(key, parseJSON2DataObject(json.getJSONObject(key),en));
					}
				}else{
					if(key.equals("lastUpdateDate")){
						data.set(key, new Date());
					}else if(key.equals("lastUpdatedBy")){
						data.set(key, ContextUtil.getCurrentUserObject().getUserId());
					}else if(key.indexOf(".")>0){
						String entityName=key.substring(0, key.indexOf("."));
						//System.out.println(entityName);
						if("mjHostel".equals(entityName)){
							DataObject dto=DataObjectUtil.createDataObject("com.mendale.hr.dormitory.dormitory.MjHostel");
							dto.set(key.substring(key.indexOf(".")+1),Integer.parseInt(json.get(key).toString()));  
							data.set("mjHostel", dto);
						}
					}else{
						data.set(key, json.get(key));
					}
				}
			}
			entities[i] = data;
		}
		return entities;
	}
	
	public static DataObject parseJSON2DataObject(JSONObject json,String entity){
		DataObject data = DataObjectUtil.createDataObject(entity);
		Iterator<String> it = json.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			Object v = json.get(key);
			data.set(key, v);
		}
		return data;
	}
	
	public static Map parseJSON2Map(JSONObject json){
		Map<String,Object> map = new HashMap<String,Object>();
		Iterator<String> it = json.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			Object v = json.get(key);
			map.put(key, v);
		}
		return map;
	}
	
	@Bizlet("字符串转化为JSONObject")
	public static JSONObject toJSONObject(String json){
		return JSON.parseObject(json);
	}
	
	public static JSONArray toJSONArray(String json){
		return JSON.parseArray(json);
	}

	public static JSONArray getJSONArrayFromObject(JSONObject jsonObject,String key){
		return jsonObject.getJSONArray(key);
	}
	
}
